﻿using Learn.net.fruit.Model;
using Microsoft.EntityFrameworkCore;


namespace Learn.net.fruit.Data
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }

        public DbSet<User> Users { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<Address> Addresses { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderDetail> OrderDetails { get; set; }
        public DbSet<Review> Reviews { get; set; }
        public DbSet<PaymentInfo> PaymentInfos { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置表关系
            modelBuilder.Entity<OrderDetail>()
                .HasKey(od => od.OrderDetailID); // 设置主键
            modelBuilder.Entity<OrderDetail>()
                .HasOne(od => od.Order)
                .WithMany(o => o.OrderDetails)
                .HasForeignKey(od => od.OrderID);

            modelBuilder.Entity<OrderDetail>()
                .HasOne(od => od.Product)
                .WithMany(p => p.OrderDetails)
                .HasForeignKey(od => od.ProductID);

            modelBuilder.Entity<Review>()
                .HasKey(r => r.ReviewID); // 设置主键
            modelBuilder.Entity<Review>()
                .HasOne(r => r.User)
                .WithMany(u => u.Reviews)
                .HasForeignKey(r => r.UserID);

            modelBuilder.Entity<Review>()
                .HasOne(r => r.Product)
                .WithMany(p => p.Reviews)
                .HasForeignKey(r => r.ProductID);

            modelBuilder.Entity<PaymentInfo>()
                .HasKey(pi => pi.OrderID); // 设置主键
            modelBuilder.Entity<PaymentInfo>()
                .HasOne(pi => pi.Order)
                .WithOne(o => o.PaymentInfo)
                .HasForeignKey<PaymentInfo>(pi => pi.OrderID);

            modelBuilder.Entity<Address>()
                .HasKey(a => a.AddressID); // 设置主键
            modelBuilder.Entity<Address>()
                .HasOne(a => a.User)
                .WithMany(u => u.Addresses)
                .HasForeignKey(a => a.UserID);

            modelBuilder.Entity<Order>()
                .HasKey(o => o.OrderID); // 设置主键
            modelBuilder.Entity<Order>()
                .HasOne(o => o.User)
                .WithMany(u => u.Orders)
                .HasForeignKey(o => o.UserID);

            modelBuilder.Entity<Product>()
                .HasKey(p => p.ProductID); // 设置主键
            modelBuilder.Entity<Product>()
                .HasOne(p => p.Category)
                .WithMany(c => c.Products)
                .HasForeignKey(p => p.CategoryID);

            // 添加其他实体的主键配置
            modelBuilder.Entity<Category>()
                .HasKey(c => c.CategoryID); // 设置主键

            modelBuilder.Entity<User>()
                .HasKey(u => u.UserID); // 设置主键
        }
    }
}